/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bbchess;

import bbchess.util.ChessTimer;
import bbchess.util.ImageLoader;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.lang.Math.*;
import javax.swing.BorderFactory;

/**
 *
 * @author Tyler
 */
public class StatusPanel extends javax.swing.JPanel {

    // declare variables
    private NameLabel whiteName;
    private NameLabel blackName;
    private ChessTimer whiteTime;
    private ChessTimer blackTime;
    private PlayerPanel whitePlayerPanel;
    private PlayerPanel blackPlayerPanel;
    private javax.swing.JLabel checkLabel;
    Image white;
    Image black;
    private int centerx;
    private int labelx;
    private int iconx;
    private int offset;

    StatusPanel() {
        super();

        whiteName = new NameLabel("Player 1");
        blackName = new NameLabel("Player 2");
        whiteTime = new ChessTimer(10);
        blackTime = new ChessTimer(10);
        whitePlayerPanel = new PlayerPanel();
        blackPlayerPanel = new PlayerPanel();
        checkLabel = new javax.swing.JLabel("");

        add(whitePlayerPanel);
        add(blackPlayerPanel);

        whitePlayerPanel.add(whiteName, java.awt.BorderLayout.NORTH);
        blackPlayerPanel.add(blackName, java.awt.BorderLayout.NORTH);

        whitePlayerPanel.add(whiteTime, java.awt.BorderLayout.SOUTH);
        blackPlayerPanel.add(blackTime, java.awt.BorderLayout.SOUTH);

        add(checkLabel);
        
        white = ImageLoader.getwPawnImage();
        black = ImageLoader.getbPawnImage();
        centerx = 212;
        labelx = 90;
        iconx = 26;
        offset = 90;

        whitePlayerPanel.setLocation(centerx - offset - iconx, 4);
        blackPlayerPanel.setLocation(centerx + offset + iconx, 4);

        checkLabel.setFont(new java.awt.Font("SERIF", java.awt.Font.PLAIN, 18));
        checkLabel.setForeground(Color.BLACK);
        checkLabel.setSize(checkLabel.getPreferredSize());
        checkLabel.setLocation(centerx - (checkLabel.getWidth()/2) + iconx, 4);
        checkLabel.setVisible(true);

        white = ImageLoader.getwPawnHighlightedImage();
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.drawImage(ImageLoader.getStatusPanelImage(), 0, 0, getWidth(), getHeight(), this);
        g.drawImage(white, centerx - offset - iconx - labelx/2, 2, getHeight(), getHeight(), this);
        g.drawImage(black, centerx + offset + iconx - labelx/2, 2, getHeight(), getHeight(), this);

    }

    public void updateTurnStatus(String player) {
        if (player.equals("white")) {
            white = ImageLoader.getwPawnHighlightedImage();
            black = ImageLoader.getbPawnImage();
            whiteTime.unpause();
            blackTime.pause();
            repaint();
        }

        else {
            white = ImageLoader.getwPawnImage();
            black = ImageLoader.getbPawnHighlightedImage();
            whiteTime.pause();
            blackTime.unpause();
            repaint();
        }
    }

    /**
     * Pauses the timers.
     */
    public void pause() {
        whiteTime.pause();
        blackTime.pause();
    }

    public void resetTimers() {
        whiteTime.pause();
        blackTime.pause();
        whiteTime.resetTimer();
        blackTime.resetTimer();
    }

    public void setCheckLabelText(String label) {
        checkLabel.setText(label);
        checkLabel.setSize(checkLabel.getPreferredSize());
        checkLabel.setLocation(centerx - (checkLabel.getWidth()/2) + iconx, 4);
    }

    // called when the screen is resizing
    public void resizePanel(int newWidth) {
        centerx = newWidth/2;
        whitePlayerPanel.setLocation(centerx - offset - iconx, 4);
        blackPlayerPanel.setLocation(centerx + offset + iconx, 4);
    }

    public void setWhiteName(String string) {
        whiteName.setText(string);
    }

    public void setBlackName(String string) {
        blackName.setText(string);
    }

    public class PlayerPanel extends javax.swing.JPanel {

        java.awt.BorderLayout layout;

        PlayerPanel() {
            setSize(80, 30);
            
            setBackground(new Color(255, 255, 255, 70));

            layout = new java.awt.BorderLayout();

            setLayout(layout);

            setBorder(BorderFactory.createLineBorder(Color.BLACK));
            setOpaque(true);
            setVisible(true);
        }
    }

    public class NameLabel extends javax.swing.JLabel {
        NameLabel(String string) {
            super(string);

            setFont(new java.awt.Font("SERIF", java.awt.Font.BOLD, 12));

            setVerticalAlignment(CENTER);
            setHorizontalAlignment(CENTER);
            setPreferredSize(new Dimension(getWidth() + 2, getHeight() + 12));
        }
    }
    
}
